#include "stdafx.h"
#include "LoggingEventHandler.h"
#include "SOCK_Stream.h"
#include <time.h>

namespace Network 
{
	LoggingEventHandler::LoggingEventHandler(const INET_Addr& ipAddress)
	{
		listener = new SOCK_Acceptor(ipAddress);
		logs.clear();
	}

	void LoggingEventHandler::handle(unsigned char* data) {
		LogEvent event;

		if(deserializeLogEvent((char*)data, &event) != 0){
			fprintf(stdout, "Error in deserialization of LogEvent!");
		}

		time_t timestamp;
		time(&(timestamp));
		
		LogListItem logListItem(event, timestamp);
		logs.push_back(logListItem);

		char buffer[26];
		ctime_s(buffer, 26, &(logListItem.timestamp));

		printf("Log received at %s with text %s \n", buffer, logListItem.event.text.c_str());
	}
}